package com.usian.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.article.feign.BehaviorFeign;
import com.usian.article.mapper.ApArticleConfigMapper;
import com.usian.article.mapper.ApArticleMapper;
import com.usian.article.service.ApArticleConfigService;
import com.usian.article.service.ApArticleContentService;
import com.usian.article.service.ApArticleService;
import com.usian.model.article.dtos.ArticleDto;
import com.usian.model.article.dtos.ArticleHomeDto;
import com.usian.model.article.dtos.ArticleInfoDto;
import com.usian.model.article.pojos.ApArticle;
import com.usian.model.article.pojos.ApArticleConfig;
import com.usian.model.article.pojos.ApArticleContent;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.behavior.pojos.ApFollowBehavior;
import com.usian.model.behavior.pojos.ApLikesBehavior;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle>
        implements ApArticleService {

    @Autowired
    ApArticleMapper apArticleMapper;

    @Autowired
    ApArticleConfigService apArticleConfigService;

    @Autowired
    ApArticleContentService apArticleContentService;

    @Value("${fileServerUrl}")
    String fileServerUrl;

    @Autowired
    BehaviorFeign behaviorFeign;

    @Override
    public ApArticle saveArticle(ApArticle apArticle) {
        apArticle.setCreatedTime(new Date());
        this.save(apArticle);
        return apArticle;
    }

    /**
     * 根据参数加载文章列表
     * @param type 1为加载更多  2为加载最新
     * @param dto
     * @return
     */
    @Override
    public ResponseResult load(Short type, ArticleHomeDto dto) {
        if (dto==null || dto.getSize()==null || dto.getMinBehotTime()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        if (StringUtils.isBlank(dto.getTag())){
            dto.setTag("__all__");
        }
        dto.setMinBehotTime(new Date());
        List<ApArticle> apArticleList = apArticleMapper.loadArticleList(dto, type);

        ResponseResult responseResult = ResponseResult.okResult(apArticleList);
        responseResult.setHost(fileServerUrl);
        return responseResult;
    }

    @Override
    public ResponseResult loadArticleInfo(ArticleDto dto) {
        if(dto==null || dto.getArticleId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        Map<String, Object> map = new HashMap<>();
        ApArticleConfig apArticleConfig = apArticleConfigService.getOne(Wrappers.<ApArticleConfig>lambdaQuery()
                .eq(ApArticleConfig::getArticleId, dto.getArticleId()));

        if (apArticleConfig!=null){
            //没下线，没删除
            if (!apArticleConfig.getIsDown() && !apArticleConfig.getIsDelete()){
                map.put("config",apArticleConfig);

                //查询文章内容
                ApArticleContent apArticleContent = apArticleContentService.getOne(Wrappers.<ApArticleContent>lambdaQuery()
                        .eq(ApArticleContent::getArticleId, dto.getArticleId()));
                map.put("content",apArticleContent);

            }
        }

        return ResponseResult.okResult(map);
    }


    /**
     * 加载文章详情的行为内容
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {

        Map<String, Object> map = new HashMap<>();
        if (dto==null || dto.getArticleId()==null || dto.getAuthorId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //获取当前用户
        ApUser apUser = AppThreadLocalUtils.getUser();
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        ApBehaviorEntry apBehaviorEntry = behaviorFeign.findByUserIdOrEquipmentId(apUser.getId(), dto.getEquipmentId());
        if (apBehaviorEntry==null){
            //关注
            List<ApFollowBehavior> apFollowBehaviorList = behaviorFeign.saveFollowBehavior(apBehaviorEntry.getEntryId(), dto.getArticleId(), dto.getAuthorId());
            if (apFollowBehaviorList.size()>0){
                map.put("isFollow",true);
            }else {
                map.put("isFollow",false);
            }

            //点赞
            ApLikesBehavior apLikesBehavior = behaviorFeign.findLike(apBehaviorEntry.getEntryId(), dto.getArticleId());
            if (apBehaviorEntry!=null){
                map.put("isLike",true);
            }else {
                map.put("isLike",false);
            }


        }

        return ResponseResult.okResult(map);
    }
}
